<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Test libmacrame - Core-DOM</title>
    <link rel="stylesheet" href="common.css" type="text/css">

    <script type="text/javascript" src="common.js"></script>
    <script type="text/javascript" src="../src/core.js"></script>
    <script type="text/javascript" src="../src/core-js.js"></script>
    <script type="text/javascript" src="../src/core-dom.js"></script>

    <script type="text/javascript"><!--
/*
 * Test and display which methods are native or not from the global DOM objects.
 */
tests.coredom_native = function () {
  var obj, arg = [], objects = ['Node', 'Element', 'Text', 'NodeList', 
      'HTMLCollection', 'NamedNodeMap', 'EventTarget'];

  for (var i = 0, n = objects.length; i < n; i++) {
    obj = objects[i];

    if (!$.dom[obj]) {
      console.warn('$.objects.' + obj + ' was not found.');
      continue;
    }

    if (window[obj] && window[obj].prototype) {
      arg.push(window[obj].prototype, $.dom[obj], obj + '.prototype');
    } else {
      console.warn('window.' + obj + ' is not available.');
    }
  }

  $t.check_native.apply($t, arg);
};

/*
 * Test the Node methods, native or not.
 */
tests.coredom_Node = function () {
  $t.output('Testing .appendChild() and .prependChild() ...');

  var pnode1 = document.createElement('div'),
    pnode2 = document.createElement('div'),
    child1 = document.createElement('p'),
    child2 = document.createElement('p');

  pnode1.id = 'pn1';
  pnode2.id = 'pn2';
  child1.id = 'ch1';
  child2.id = 'ch2';

  pnode1.appendChild(child2);

  if (pnode1.firstChild.id != child2.id) {
    $t.report_error('pnode1.appendChild(child2) failed.');
  }

  pnode1.prependChild(child1);

  if (pnode1.firstChild.id != child1.id) {
    $t.report_error('pnode1.prependChild(child1) failed.');
  }

  $t.output('Testing .removeFirstChild() and .removeLastChild() ...');

  if (pnode1.removeFirstChild().id != child1.id) {
    $t.report_error('pnode1.removeFirstChild() failed.');
  }

  if (pnode1.removeLastChild().id != child2.id) {
    $t.report_error('pnode1.removeLastChild() failed.');
  }

  $t.output('Testing .appendTo() and .prependTo() ...');

  child2.appendTo(pnode2);
  if (pnode2.lastChild.id != child2.id) {
    $t.report_error('child2.appendTo(pnode2) failed.');
  }

  child1.prependTo(pnode2);

  if (pnode2.firstChild.id != child1.id) {
    $t.report_error('child1.prependTo(pnode2) failed.');
  }

  $t.output('Testing .insertAfter() ...');

  pnode2.insertAfter(pnode1, child1);
  if (pnode1.parentNode.id != pnode2.id || child1.nextSibling.id != pnode1.id || 
      pnode1.nextSibling.id != child2.id) {
    $t.report_error('pnode2.insertAfter(pnode1, child1) failed');
  }
};

/*
 * Test the Element methods, native or not.
 */
tests.coredom_Element = function () {
  $t.output('Testing .wrap() ...');

  var child1 = document.createElement('p');

  child1.id = 'ch1';

  var str1 = 'test1 <div id="wrapper1">test2 ' +
    '<div id="wrapper2" class="class1">test3</div> test4</div> test5';

  var wrapper1 = child1.wrap(str1);

  $t.check_cond(wrapper1.id, 'wrapper1', '==');

  var wrapper2 = child1.parentNode;

  $t.check_cond(wrapper2.id, 'wrapper2', '==');
  $t.check_cond(wrapper2.className, 'class1', '==');
  $t.check_cond(wrapper1.firstElementChild.id, wrapper2.id, '==');
  $t.check_cond(wrapper1.firstChild.nodeValue, 'test2 ', '==');
  $t.check_cond(wrapper1.lastChild.nodeValue, ' test4', '==');

  wrapper2.removeChild(child1);

  var str2 = '<div><custom>';
  wrapper1 = child1.wrap(str2);

  $t.check_cond(wrapper1.nodeName.toLowerCase(), 'div', '==');

  wrapper2 = wrapper1.firstChild;

  $t.check_cond(wrapper2.nodeName.toLowerCase(), 'custom', '==');
  $t.check_cond(child1.parentNode.nodeName.toLowerCase(), 'custom', '==');

  wrapper2.removeChild(child1);

  $t.output('Testing .wrapInner() ...');

  var pnode1 = document.createElement('div'),
      child2 = document.createElement('p');

  child2.id = 'ch2';
  pnode1.appendChild(child1);
  pnode1.appendChild(child2);

  wrapper1 = pnode1.wrapInner('<div class="wrapper">');

  $t.check_cond(wrapper1 instanceof NodeList, true, '===');
  $t.check_cond(wrapper1[0].id, child1.id, '==');
  $t.check_cond(wrapper1[1].id, child2.id, '==');
  $t.check_cond(wrapper1[0].parentNode.className, 'wrapper', '==');
  $t.check_cond(child1.nextSibling.id, child2.id, '==');
  $t.check_cond(pnode1.childNodes.length, 1, '==');
};

function get_pnode (n) {
  var i, elem, pnode = document.createElement('ul');

  for (i = 0; i < n; i++) {
    elem = document.createElement('li');
    elem.id = 'li' + i;
    pnode.appendChild(elem);
  }

  return pnode;
}

function nr_lt_5 (elem) {
  return parseInt(elem.id.replace('li', '')) < 5;
}

function nr_gt_10 (elem) {
  return parseInt(elem.id.replace('li', '')) > 9;
};

function is_li (elem, i) {
  return elem.id == 'li' + i;
}


/*
 * Test the NodeList Array methods.
 */

tests.coredom_NodeList_toArray = function () {
  var n = 5;
  var pnode = get_pnode(n);
  var list = pnode.childNodes;

  var arr = list.toArray();
  $t.check_cond(arr instanceof Array, true, '===');
  $t.check_cond(arr.length, list.length, '==');
  $t.check_cond(arr.length, n, '==');

  if (!arr.every(is_li)) {
    $t.report_error('list.toArray() failed.');
  }
};


tests.coredom_NodeList_every = function () {
  var n = 6;
  var pnode = get_pnode(n);
  var list = pnode.childNodes;

  $t.check_cond(list instanceof NodeList, true, '===');
  $t.check_cond(list.length, n, '==');

  if (!list.every(is_li)) {
    $t.report_error('list.every(is_li) failed.');
  }

  if (list.every(nr_lt_5)) {
    $t.report_error('list.every(nr_lt_5) failed.');
  }
};

tests.coredom_NodeList_filter = function () {
  var n = 7;
  var pnode = get_pnode(n);
  var list = pnode.childNodes;

  var arr = list.filter(nr_lt_5);

  $t.check_cond(arr instanceof Array, true, '===');
  $t.check_cond(arr.length, 5, '==');

  if (!arr.every(nr_lt_5)) {
    $t.report_error('list.filter(nr_lt_5) failed.');
  }
};

tests.coredom_NodeList_some = function () {
  var n = 7;
  var pnode = get_pnode(n);
  var list = pnode.childNodes;

  if (!list.some(nr_lt_5)) {
    $t.report_error('list.some(nr_lt_5) failed.');
  }

  if (list.some(nr_gt_10)) {
    $t.report_error('list.some(nr_gt_10) failed.');
  }
};

tests.coredom_NodeList_map = function () {
  var n = 5;
  var pnode = get_pnode(n);
  var list = pnode.childNodes;

  var arr = list.map(function (elem) {
    return parseInt(elem.id.replace('li', '')) * 2;
  });

  var res = arr.every(function (val, i) {
    return val === i*2;
  });

  if (!res) {
    $t.report_error('list.map() failed.');
  }
};

tests.coredom_NodeList_reduce = function () {
  var n = 5;
  var pnode = get_pnode(n);
  var list = pnode.childNodes;

  var reduce_fn = function (prev, elem) {
    return prev + elem.id;
  };

  var str = '-';
  list.forEach(function (elem) {
    str += elem.id;
  });

  if (list.reduce(reduce_fn, '-') != str) {
    $t.report_error('list.reduce() failed.');
  }

  str = '';
  list.forEach(function (elem) {
    str = elem.id + str;
  });
  str = '-' + str;

  if (list.reduceRight(reduce_fn, '-') != str) {
    $t.report_error('list.reduceRight() failed.');
  }
};

tests.coredom_NodeList_slice = function () {
  var n = 5;
  var pnode = get_pnode(n);
  var list = pnode.childNodes;

  var arr1 = list.slice(2, 3),
      arr2 = list.toArray().slice(2, 3);

  $t.check_cond(arr1 instanceof Array, true, '===');
  $t.check_cond(arr1.length, arr2.length, '==');
  $t.check_cond(arr1[0].id, 'li2', '==');
  $t.check_cond(arr1[0].id, arr2[0].id, '==');

  arr1 = list.slice(3);

  $t.check_cond(arr1 instanceof Array, true, '===');
  $t.check_cond(arr1.length, n - 3, '==');
  $t.check_cond(arr1[0].id, 'li3', '==');
  $t.check_cond(arr1[1].id, 'li4', '==');
};

/*
 * Test the NodeList class methods.
 */
tests.coredom_NodeList_class = function () {
  var n = 5;
  var pnode = get_pnode(n);
  var list = pnode.childNodes;

  list.addClass('testClass');

  var res = list.every(function (elem) {
    return elem.classList.has('testClass');
  });

  if (!res) {
    $t.report_error('list.addClass() failed.');
  }

  if (!list.hasClass('testClass')) {
    $t.report_error('list.hasClass("testClass") failed.');
  }

  if (list.hasClass('testClass2')) {
    $t.report_error('list.hasClass("testClass2") failed.');
  }

  list.toggleClass('testClass');
  list.toggleClass('testClass2');

  if (list.hasClass('testClass')) {
    $t.report_error('list.toggleClass("testClass") failed.');
  }

  if (!list.hasClass('testClass2')) {
    $t.report_error('list.toggleClass("testClass2") failed.');
  }

  list.removeClass('testClass2');

  if (list.hasClass('testClass2')) {
    $t.report_error('list.removeClass("testClass2") failed.');
  }
};

/*
 * Test the NodeList attribute methods.
 */
tests.coredom_NodeList_attribute = function () {
  var n = 5;
  var pnode = get_pnode(n);
  var list = pnode.childNodes;

  list.setAttribute('test', 'val');

  var res = list.every(function (elem) {
    return elem.getAttribute('test') == 'val';
  });

  if (!res) {
    $t.report_error('list.setAttribute(ns, "test", "val") failed.');
  }

  if (!list.hasAttribute('test')) {
    $t.report_error('list.hasAttribute("test") failed.');
  }

  if (list.hasAttribute('test2')) {
    $t.report_error('list.hasAttribute("test2") failed.');
  }

  list.removeAttribute('test');

  if (list.hasAttribute('test')) {
    $t.report_error('list.removeAttribute("test") failed.');
  }
};

/*
 * Test the NodeList attribute methods with namespaces.
 */
tests.coredom_NodeList_attributeNS = function () {
  var n = 5;
  var pnode = get_pnode(n);
  var list = pnode.childNodes,
      ns = 'http://ns.example.com/';

  list.setAttributeNS(ns, 'test', 'val');

  var res = list.every(function (elem) {
    return elem.getAttributeNS(ns, 'test') == 'val';
  });

  if (!res) {
    $t.report_error('list.setAttributeNS(ns, "test", "val") failed.');
  }

  if (!list.hasAttributeNS(ns, 'test')) {
    $t.report_error('list.hasAttributeNS(ns, "test") failed.');
  }

  if (list.hasAttributeNS(ns, 'test2')) {
    $t.report_error('list.hasAttributeNS(ns, "test2") failed.');
  }

  if (list.hasAttributeNS(ns+2, 'test')) {
    $t.report_error('list.hasAttributeNS(ns+2, "test") failed.');
  }

  list.setAttributeNS(ns+2, 'test', 'val2');

  res = list.every(function (elem) {
    return elem.getAttributeNS(ns+2, 'test') == 'val2';
  });

  if (!res) {
    $t.report_error('list.setAttributeNS(ns+2, "test", "val2") failed.');
  }

  if (!list.hasAttributeNS(ns+2, 'test')) {
    $t.report_error('list.hasAttributeNS(ns+2, "test") failed.');
  }

  list.removeAttributeNS(ns, 'test');
  list.removeAttributeNS(ns+2, 'test');

  if (list.hasAttributeNS(ns, 'test')) {
    $t.report_error('list.removeAttributeNS(ns, "test") failed.');
  }

  if (list.hasAttributeNS(ns+2, 'test')) {
    $t.report_error('list.removeAttributeNS(ns+2, "test") failed.');
  }
};

/*
 * Test the methods which allow DOM tree manipulation in a NodeList.
 */

tests.coredom_NodeList_appendChild = function () {
  var n = 5;
  var pnode = get_pnode(n);
  var list = pnode.childNodes,
    child1 = document.createElement('p'),
    child2 = document.createElement('p');

  child1.className = 'ch1';
  child2.className = 'ch2';

  list.appendChild(child1);
  list.appendChild(child2);

  var res = list.every(function (elem) {
    return elem.firstChild.className == child1.className && 
    elem.lastChild.className == child2.className;
  });

  if (!res) {
    $t.report_error('list.appendChild() failed.');
  }
};

tests.coredom_NodeList_prependChild = function () {
  var n = 5;
  var pnode = get_pnode(n);
  var list = pnode.childNodes,
    child1 = document.createElement('p'),
    child2 = document.createElement('p');

  child1.className = 'ch1';
  child2.className = 'ch2';

  list.prependChild(child2);
  list.prependChild(child1);

  res = list.every(function (elem) {
    return elem.firstChild.className == child1.className && 
    elem.lastChild.className == child2.className;
  });

  if (!res) {
    $t.report_error('list.prependChild() failed.');
  }
};

function children_fn (elem) {
  return elem.className == 'ch1' || elem.className == 'ch2';
};

tests.coredom_NodeList_children = function () {
  var n = 5;
  var pnode = get_pnode(n);
  var list = pnode.childNodes,
    child1 = document.createElement('p'),
    child2 = document.createElement('p');

  child1.className = 'ch1';
  child2.className = 'ch2';

  list.appendChild(child1);
  list.appendChild(child2);

  // Get all the child elements of the NodeList. This returns an array of all 
  // the paragraph elements just added to the LIs.
  var arr = list.children();

  $t.check_cond(arr instanceof Array, true, '===');
  $t.check_cond(arr.length, n * 2, '==');

  if (!arr.every(children_fn)) {
    $t.report_error('list.children() failed: arr.every(children_fn) returned ' +
        'false.');
  }
};

tests.coredom_NodeList_empty = function () {
  var n = 5;
  var pnode = get_pnode(n);
  var list = pnode.childNodes,
    child1 = document.createElement('p'),
    child2 = document.createElement('p');

  child1.className = 'ch1';
  child2.className = 'ch2';

  list.appendChild(child1);
  list.appendChild(child2);

  var arr = list.empty();

  $t.check_cond(arr instanceof Array, true, '===');
  $t.check_cond(arr.length, n * 2, '==');

  if (!arr.every(children_fn)) {
    $t.report_error('list.empty() failed returning the removed elements: ' +
        'arr.every(children_fn) returned false.');
  }

  if (list.children().length !== 0) {
    $t.report_error('list.empty() failed because list.children() does not return ' +
        'an empty array.');
  }
};


tests.coredom_NodeList_removeFirstChild = function () {
  var n = 5;
  var pnode = get_pnode(n);
  var list = pnode.childNodes,
    child1 = document.createElement('p'),
    child2 = document.createElement('p');

  child1.className = 'ch1';
  child2.className = 'ch2';

  list.appendChild(child1);
  list.appendChild(child2);

  var arr = list.removeFirstChild();
  $t.check_cond(arr instanceof Array, true, '===');
  $t.check_cond(arr.length, n, '==');

  var res = arr.every(function (elem) {
    return elem.className == child1.className;
  });

  if (!res) {
    $t.report_error('list.removeFirstChild() failed because arr.every() ' +
        'returned false.');
  }

  res = list.every(function (elem) {
    return elem.firstChild.className == child2.className;
  });

  if (!res) {
    $t.report_error('list.removeFirstChild() failed because list.every() ' +
        'returned false.');
  }
};

tests.coredom_NodeList_removeLastChild = function () {
  var n = 5;
  var pnode = get_pnode(n);
  var list = pnode.childNodes,
    child1 = document.createElement('p'),
    child2 = document.createElement('p');

  child1.className = 'ch1';
  child2.className = 'ch2';

  list.appendChild(child1);
  list.appendChild(child2);

  var arr = list.removeLastChild();

  $t.check_cond(arr instanceof Array, true, '===');
  $t.check_cond(arr.length, n, '==');

  var res = arr.every(function (elem) {
    return elem.className == child2.className;
  });

  if (!res) {
    $t.report_error('list.removeLastChild() failed because arr.every() ' +
        'returned false.');
  }

  res = list.every(function (elem) {
    return elem.lastChild.className == child1.className;
  });

  if (!res) {
    $t.report_error('list.removeLastChild() failed because list.every() ' +
        'returned false.');
  }
};

tests.coredom_NodeList_prependTo = function () {
  var n = 3;
  var pnode = get_pnode(n);
  var list = pnode.childNodes,
    child1 = document.createElement('p'),
    child2 = document.createElement('p'),
    dest = document.createElement('div');

  child1.className = 'ch1';
  child2.className = 'ch2';

  dest.appendChild(child1);
  dest.appendChild(child2);

  $t.check_cond(dest.firstChild.className, child1.className, '==');
  $t.check_cond(dest.lastChild.className, child2.className, '==');

  list.prependTo(dest);

  $t.check_cond(list.length, 0, '===');

  list = dest.childNodes;

  $t.check_cond(list.length, n + 2, '===');
  $t.check_cond(list[0].id, 'li0', '==');
  $t.check_cond(list[1].id, 'li1', '==');
  $t.check_cond(list[2].id, 'li2', '==');
  $t.check_cond(list[3].className, child1.className, '==');
  $t.check_cond(list[4].className, child2.className, '==');
  $t.check_cond(child2.previousSibling.className, child1.className, '==');
  $t.check_cond(dest.lastChild.className, child2.className, '==');
};

tests.coredom_NodeList_appendTo = function () {
  var n = 3;
  var pnode = get_pnode(n);
  var list = pnode.childNodes,
    child1 = document.createElement('p'),
    child2 = document.createElement('p'),
    dest = document.createElement('div');

  child1.className = 'ch1';
  child2.className = 'ch2';

  dest.appendChild(child1);
  dest.appendChild(child2);

  $t.check_cond(dest.firstChild.className, child1.className, '==');
  $t.check_cond(dest.lastChild.className, child2.className, '==');

  list.appendTo(dest);

  $t.check_cond(list.length, 0, '===');

  list = dest.childNodes;

  $t.check_cond(list.length, n + 2, '===');
  $t.check_cond(dest.firstChild.className, child1.className, '==');
  $t.check_cond(child1.nextSibling.className, child2.className, '==');
  $t.check_cond(list[0].className, child1.className, '==');
  $t.check_cond(list[1].className, child2.className, '==');
  $t.check_cond(list[2].id, 'li0', '==');
  $t.check_cond(list[3].id, 'li1', '==');
  $t.check_cond(list[4].id, 'li2', '==');
};

tests.coredom_NodeList_slice = function () {
  var n = 6;
  var pnode = get_pnode(n);
  var list = pnode.childNodes;

  var arr = list.slice(0, 2);

  $t.check_cond(arr instanceof Array, true, '===');
  $t.check_cond(arr.length, 2, '===');
  $t.check_cond(arr[0].id, 'li0', '==');
  $t.check_cond(arr[1].id, 'li1', '==');

  arr = list.slice(2, 5);

  $t.check_cond(arr instanceof Array, true, '===');
  $t.check_cond(arr.length, 3, '===');
  $t.check_cond(arr[0].id, 'li2', '==');
  $t.check_cond(arr[1].id, 'li3', '==');
  $t.check_cond(arr[2].id, 'li4', '==');

  arr = list.slice(3);

  $t.check_cond(arr instanceof Array, true, '===');
  $t.check_cond(arr.length, 3, '===');
  $t.check_cond(arr[0].id, 'li3', '==');
  $t.check_cond(arr[1].id, 'li4', '==');
  $t.check_cond(arr[2].id, 'li5', '==');
};


tests.coredom_NodeList_insertBefore = function () {
  var n = 3;
  var pnode = get_pnode(n);
  var list = pnode.childNodes,
    child1 = document.createElement('p'),
    child2 = document.createElement('p'),
    dest = document.createElement('div');

  child1.className = 'ch1';
  child2.className = 'ch2';

  dest.appendChild(child1);
  dest.appendChild(child2);

  list = list.insertBefore(child2);

  $t.check_cond(list.length, n + 2, '==');

  $t.check_cond(pnode.hasChildNodes(), false, '===');
  $t.check_cond(list[0].className, child1.className, '==');
  $t.check_cond(list[1].id, 'li0', '==');
  $t.check_cond(list[2].id, 'li1', '==');
  $t.check_cond(list[3].id, 'li2', '==');
  $t.check_cond(list[4].className, child2.className, '==');
};

tests.coredom_NodeList_insertAfter = function () {
  var n = 3;
  var pnode = get_pnode(n);
  var list = pnode.childNodes,
    child1 = document.createElement('p'),
    child2 = document.createElement('p'),
    dest = document.createElement('div');

  child1.className = 'ch1';
  child2.className = 'ch2';

  dest.appendChild(child1);
  dest.appendChild(child2);

  list = list.insertAfter(child1);

  $t.check_cond(list.length, n + 2, '==');

  $t.check_cond(pnode.hasChildNodes(), false, '===');
  $t.check_cond(list[0].className, child1.className, '==');
  $t.check_cond(list[1].id, 'li0', '==');
  $t.check_cond(list[2].id, 'li1', '==');
  $t.check_cond(list[3].id, 'li2', '==');
  $t.check_cond(list[4].className, child2.className, '==');
};

tests.coredom_NodeList_wrapEach = function () {
  function run () {
    var n = 3;
    var pnode = get_pnode(n);
    var list = pnode.childNodes;

    list.wrapEach(wrapper);

    $t.check_cond(list.length, n, '==');

    var arr = list.children();

    $t.check_cond(arr.length, n, '==');

    if (!arr.every(is_li)) {
      $t.report_error('list.wrapEach(wrapper) failed because ' +
          'list.children().every(is_li) returned false.');
    }

    var res = list.every(function (elem) {
      return elem.className == 'wp';
    });

    if (!res) {
      $t.report_error('list.wrapEach(wrapper) failed because list.every() ' +
          'returned false.');
    }
  }


  $t.output('Testing .wrapEach(node) ... ');

  var wrapper = document.createElement('p');
  wrapper.className = 'wp';

  run();


  $t.output('Testing .wrapEach(string) ... ');

  wrapper = '<div class="wp">';

  run();
};

tests.coredom_NodeList_wrapAll = function () {
  function run () {
    var n = 3;
    var pnode = get_pnode(n);
    var list = pnode.childNodes;

    var list2 = list.wrapAll(wrapper);

    $t.check_cond(list.length, 1, '==');
    $t.check_cond(list[0].className, 'wp', '==');

    var arr = list.children();
    $t.check_cond(arr instanceof Array, true, '===');
    $t.check_cond(arr.length, n, '==');

    if (!arr.every(is_li)) {
      $t.report_error('list.wrapAll(wrapper) failed because ' +
          'list.children().every(is_li) returned false.');
    }

    $t.check_cond(list2 instanceof NodeList, true, '===');
    $t.check_cond(list2.length, n, '==');

    if (!list2.every(is_li)) {
      $t.report_error('list.wrapAll(wrapper) failed because ' +
          'list2.every(is_li) returned false.');
    }
  }


  $t.output('Testing .wrapAll(node) ... ');

  var wrapper = document.createElement('p');
  wrapper.className = 'wp';

  run();


  $t.output('Testing .wrapAll(string) ... ');

  wrapper = '<div class="wp">';

  run();
};

tests.coredom_NodeList_wrapInner = function () {
  function run () {
    var n = 3;
    var pnode = get_pnode(n);
    var list = pnode.childNodes,
        child1 = document.createElement('p'),
        child2 = document.createElement('p');

    child1.className = 'ch1';
    child2.className = 'ch2';

    list.appendChild(child1);
    list.appendChild(child2);

    list.wrapInner(wrapper);

    $t.check_cond(list.length, n, '==');

    if (!list.every(is_li)) {
      $t.report_error('list.wrapInner(wrapper) failed because ' +
          'list.every(is_li) returned false.');
    }

    var arr = list.children();

    $t.check_cond(arr.length, n, '==');

    var res = arr.every(function (elem) {
      return elem.className == 'wp';
    });

    if (!res) {
      $t.report_error('list.wrapInner() failed because ' +
        'arr.every() returned false. Could not find the wrapper elements.');
    }

    res = arr.every(function (elem) {
      return elem.firstChild.className == 'ch1' &&
        elem.lastChild.className == 'ch2';
    });

    if (!res) {
      $t.report_error('list.wrapInner() failed because ' +
        'arr.every() returned false. Could not find the wrapper child ' +
        'elements.');
    }
  }


  $t.output('Testing .wrapInner(node) ... ');

  var wrapper = document.createElement('p');
  wrapper.className = 'wp';

  run();


  $t.output('Testing .wrapInner(string) ... ');

  wrapper = '<div class="wp">';

  run();
};

tests.coredom_NodeList_style = function () {
  $t.output('Testing .style("property", "value") ...');

  var pnode = $('#NodeList_style');
  pnode.childNodes.style('background', '#cfc');

  $t.output('Testing .style({"property1" : "value1", ' +
    '"property2" : "value2"}) ...');

  pnode.childNodes.style({'background' : 'transparent', 'color' : '#0a0'});

  $t.output('Testing .style("property") ...');

  var padding = pnode.childNodes.style('paddingLeft');

  $t.check_cond(padding, '13px', '==');
};

tests.coredom_getStyle = function () {
  var elems = $('#NodeList_style > li');
  var padding = $.dom.getStyle(elems[0], 'paddingLeft');

  $t.check_cond(padding, '13px', '==');
};

// This test simply sets up some event listeners. You see the events in the 
// JavaScript console.
tests.coredom_event = function () {
  var fn = function (ev) {
    window._events.push(ev);

    var res = ev.type + "\n" +
        ' keyCode  ' + ev.keyCode +
        ' charCode  ' + ev.charCode +
        ' which ' + ev.which + "\n" +
        ' keyCode_ ' + ev.keyCode_ +
        ' charCode_ ' + ev.charCode_ +
        ' kid ' + ev.keyIdentifier +
        ' key_ ' + ev.key_ +
        ' char_ ' + ev.char_ + "\n" +
        ' repeat_ ' + ev.repeat_;

    // Force getKey() to run.
    ev.key_ = ev.char_ = null;
    if (ev.getKey) {
      res += ' getKey ' + ev.getKey();
    } else {
      res += ' getKey! ' + $.dom.KeyboardEvent.getKey.call(ev);
    }

    // Test preventDefault() consistency across browsers.
    if (ev.type == 'keypress' && ev.preventDefault) {
      ev.preventDefault();
    }

    console.log(res);
  };

  window._events = [];

  var elem = $('#NodeList_style');
  elem.addEventListener('click', fn, false);

  var kb = new $.dom.KeyboardEventListener(
      $.browser.gecko ? window : document.body,
      {keydown: fn, keypress: fn, keyup: fn});

  //window.addEventListener('keydown', fn, false);
  //window.addEventListener('keypress', fn, false);
  //window.addEventListener('keyup', fn, false);
};
    // --></script>

    <style type="text/css"><!--
#NodeList_style li {
  padding-left: 13px;
  background: #fcc;
  color: #f00;
  font-weight: bold;
}
    --></style>

  </head>
  <body>
    <ul id="NodeList_style">
      <li>test</li>
      <li>test</li>
      <li>test</li>
    </ul>
  </body>
  <!-- vim:set spell spl=en fo=tcroqwanl1 tw=80 ts=2 sw=2 sts=2 sta et ai cin ff=unix: -->
</html>
